<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Watchdog library
 *
 * This file contains some useful functions to do logging
 *
 * PHP version 5.2
 *
 * @category Frameworks
 * @package  Omifk
 * @author   Claudio Beatrice <claudi0.beatric3@gmail.com>
 * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
 * @version  SVN: $Id$
 * @link     http://www.silent-voice.org/
 */

defined('STDERR') || define('STDERR', fopen('php://stderr', 'w'));
defined('STDOUT') || define('STDOUT', fopen('php://stdout', 'w'));
defined('OUTPUT') || define('OUTPUT', fopen('php://output', 'w'));

/**
 *
 * Severity levels, as defined in RFC 3164 http://www.faqs.org/rfcs/rfc3164.html
 * @see watchdog()
 * @see watchdog_severity_levels()
 */
define('WATCHDOG_EMERG',    0);	// Emergency: system is unusable
define('WATCHDOG_ALERT',    1);	// Alert: action must be taken immediately
define('WATCHDOG_CRITICAL', 2); // Critical: critical conditions
define('WATCHDOG_ERROR',    3); // Error: error conditions
define('WATCHDOG_WARNING',  4);	// Warning: warning conditions
define('WATCHDOG_NOTICE',   5);	// Notice: normal but significant condition
define('WATCHDOG_INFO',     6);	// Informational: informational messages
define('WATCHDOG_DEBUG',    7);	// Debug: debug-level messages

define('WATCHDOG_DEFAULT_TYPE',        'Watchdog message');
define('WATCHDOG_DEFAULT_DEBUG_LEVEL', WATCHDOG_DEBUG);
define('WATCHDOG_DEFAULT_LOGS',        STDERR);

$logs          = WATCHDOG_DEFAULT_LOGS;
$debug_level   = WATCHDOG_DEFAULT_DEBUG_LEVEL;
$output_stream = OUTPUT;

/**
 * Print to screen and to logfile a message
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 *
 */ 
function watchdog($message, 
                  $severity = WATCHDOG_NOTICE,
                  array $variables = array(), 
                  $type = WATCHDOG_DEFAULT_TYPE) {
    global $debug_level;

    if ($severity <= $debug_level) {
        watchdog_log($message, $severity, $variables, $type);
        watchdog_echo($message, $severity, $variables, $type);
    }
    
    if ($debug_level >= WATCHDOG_DEBUG) {
        var_dump(debug_backtrace());
    }
}

/**
 * Print a log message on a the log file
 *
 * @global resource $logs
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 */
function watchdog_log($message,
                      $severity = WATCHDOG_NOTICE,
                      array $variables = array(),
                      $type = WATCHDOG_DEFAULT_TYPE) {
    global $logs;

    if (is_resource($logs)) {
        watchdog_fprintf(
            $logs, 
            watchdog_create_log_message(
                $message,
                $severity,
                $variables,
                $type
            )
        );
    }
}

/**
 * Print a log message on the screen
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 */
function watchdog_echo($message,
                       $severity = WATCHDOG_NOTICE,
                       array $variables = array(),
                       $type = WATCHDOG_DEFAULT_TYPE) {
    global $output_stream;

    watchdog_fprintf(
        $output_stream, 
        watchdog_create_log_message(
            $message,
            $severity,
            $variables,
            $type
        )
    );
}

/**
 * Helper function
 *
 * @param resource $stream
 * @param array $log_message
 */
function watchdog_fprintf($stream, array $log_message) {
    fprintf(
        $stream, 
        "%s - %s - %s\n",
        date('Y/m/d H:i:s'),
        ucfirst($log_message['severity']),
        $log_message['message']
    );
}

/**
 * Create an array structure containing info about logging
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 * @return array
 */
function watchdog_create_log_message($message = 'Error',
                                     $severity = WATCHDOG_NOTICE,
                                     array $variables = array(),
                                     $type = WATCHDOG_DEFAULT_TYPE) {
    return array(
        'message'   => $message,
        'severity'  => watchdog_get_severity_level($severity),
        'type'      => $type,
        'variables' => $variables,
        'timestamp' => time(),
    );
}

/**
 * Init some watchdog variables
 *
 * @param $custom_debug_level integer containing the level of information to show,
 * @param $custom_output resource containing the stream for the output
 * @param $custom_logs resource containing the stream for logging
 *
 * @return null
 */
function watchdog_init($custom_debug_level = WATCHDOG_DEFAULT_DEBUG_LEVEL,
                       $custom_output = OUTPUT,
                       $custom_logs = WATCHDOG_DEFAULT_LOGS) {
    global $debug_level, $output_stream, $logs;

    $debug_level   = $custom_debug_level;
    $output_stream = $custom_output;
    $logs          = $custom_logs;
}

/**
 * Return all the watchdog security levels
 *
 * @return array containing watchdog security levels
 */
function watchdog_severity_levels() {
    return array(
        WATCHDOG_EMERG    => 'emergency',
        WATCHDOG_ALERT    => 'alert',
        WATCHDOG_CRITICAL => 'critical',
        WATCHDOG_ERROR    => 'error',
        WATCHDOG_WARNING  => 'warning',
        WATCHDOG_NOTICE   => 'notice',
        WATCHDOG_INFO     => 'info',
        WATCHDOG_DEBUG    => 'debug',
    );
}

/**
 * Return the name of a severity level id
 *
 * @param int $severity
 * @return string
 *   On failure return null
 */
function watchdog_get_severity_level($severity) {
    $levels = watchdog_severity_levels();

    return (isset($levels[$severity])) ? $levels[$severity] : null;
}
